﻿using Abp.Authorization;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Basics.FileSystem.Dto;
using shenghua.Common.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.FileSystem
{
    /// <summary>
    /// 上传功能不在这里，在mvc里
    /// </summary>
    [AbpAuthorize(PermissionNames.ResourceManagement, PermissionNames.Member)]
    public class FileSystemAppService: shenghuaAppServiceBase, IFileSystemAppService
    {
        private readonly IRepository<FileItem> _fileItemRepository;
        private readonly FileSystemManager _fileSystemManager;

        public FileSystemAppService(
            IRepository<FileItem> fileItemRepository,
            FileSystemManager fileSystemManager
            )
        {
            _fileItemRepository = fileItemRepository;
            _fileSystemManager = fileSystemManager;
        }

        public async Task<SearchResultDto<FileItemDto>> Search(SearchFileItemDto input)
        {
            var query = _fileItemRepository.GetAll().Where(r => r.UserId == UserId).Where(r => r.ParentId == input.ParentId);

            if(input.IsOnlyFolder)
            {
                query = query.Where(r => r.IsFolder == true);
            }

            if(input.Type != null)
            {
                query = query.Where(r => r.Type == input.Type);
            }

            int totalCount = query.Count();
            var items = query.OrderByDescending(r => r.CreationTime).Skip(input.SkipCount).Take(input.MaxResultCount).ToList();

            return new SearchResultDto<FileItemDto>
            {
                TotalCount = totalCount,
                Items = items.Select(r => new FileItemDto(r)).ToList()
            };
        }

        public async Task MoveTo(FileItemMoveToDto input)
        {
            foreach(var id in input.Ids)
            {
                await _fileSystemManager.MoveTo(id, input.TargetId);
            }
        }

        [HttpPost]
        public async Task Delete(BatchIntIdDto input)
        {
            foreach(var id in input.Ids)
            {
                await _fileSystemManager.Delete(id);
            }
        }

        public async Task<FileItemDto> Create(CreateFileItemDto input)
        {
            FileItem item = new FileItem
            {
                DisplayName = input.DisplayName,
                Labels = new Common.CustomValueType.ValueArray<string>(input.Labels),
                Path = input.Path,
            };
            await _fileSystemManager.CreateFile(UserId, item, input.ParentId);
            return new FileItemDto(item);
        }

        public async Task<FileItemDto> Update(UpdateFileItemDto input)
        {
            FileItem item = await _fileSystemManager.Get(input.Id);
            item.DisplayName = input.DisplayName;
            item.Labels = new Common.CustomValueType.ValueArray<string>(input.Labels);
            await _fileSystemManager.Update(item);
            return new FileItemDto(item);
        }

        public async Task<FileItemDto> CreateFolder(CreateFolderDto input)
        {
            FileItem item = new FileItem
            {
                DisplayName = input.DisplayName,
                Labels = new Common.CustomValueType.ValueArray<string>(input.Labels),
            };
            await _fileSystemManager.CreateFolder(UserId, item, input.ParentId);
            return new FileItemDto(item);
        }
    }
}
